local LOCALE="frFR"

--[[ Setup the simulation environment.
The local variable "env" provides the smallest environment to allow
AceLibrary and AceLocale to work correctly.
]]
local env
do
	local dummy = function () end
	local dummy0 = function () return 0 end
	env = setmetatable({
		getfenv = function () return env end,
		GetAddOnInfo = dummy,
		IsAddOnLoadOnDemand = dummy,
		GetNumAddOns = dummy0,
		geterrorhandler = function() return error end,
		CreateFrame = function () return {
			SetScript = dummy,
			UnregisterAllEvents = dummy, 
			RegisterEvent = dummy,
			Show = dummy,
		} end,
		debugstack = function() return "" end,
		GetTime = dummy0,
		GetLocale = function() return LOCALE end,
		date = os.date,
		DEFAULT_CHAT_FRAME = {
			AddMessage = function (_, s) print(s) end,
		},
	}, {
		__index = function(tbl, key)
			local v = _G[key]
			tbl[key] = v
			return v
		end,
	})
end

--[[ Load WoW Compatible lua source files.
The function dofile() is re-implemented to accept the unicode
marker at the start of the file.
]]
local dofile
do
	local file, first
	local handler = function ()
		if first then
			first = false
			local line = file:read()
			if line:sub(1, 3) == "\239\187\191" then
				return line:sub(4).."\n"
			else
				return line.."\n"
			end
		else
			local line = file:read()
			if line then
				return line.."\n"
			end
		end
	end
	
	local loadfile = function (filename)
		local err
		file, err = io.open(filename, "r")
		if not file then
			return nil, err
		end
		first = true
		return load(handler, filename)
	end

	dofile = function (filename)
		local func, msg = loadfile(filename)
		if not func then
			error(msg)
		else
			setfenv(func, env)
			func()
		end
	end
end

--[[ Now we can load AceLibrary and AceLocale ]]
dofile("dev/Ace2/AceLibrary/AceLibrary.lua")
dofile("dev/Ace2/AceLocale-2.2/AceLocale-2.2.lua")

--[[ Force Debugging.
We hack into AceLocale to force debug output. This is done by hooking
AceLocale:new().
]]
setfenv(function ()
	local l = AceLibrary("AceLocale-2.2")
	local old = l.new
	l.new = function (...)
		local module = old(...)
		if module then
			module:EnableDebugging()
		end
		return module
	end
end, env)()

--[[ Check spell icons
If Babble-Spell is loaded, then we force the check of icons.
This is highly hackish.
]]
local function checkspellicons()
	if not env.AceLibrary:HasInstance("Babble-Spell-2.2") then return end
	local bs = env.AceLibrary("Babble-Spell-2.2")
	local al = env.AceLibrary("AceLocale-2.2")
	local TRANSLATION_TABLES = rawget(al, "TRANSLATION_TABLES")
	local tbl = rawget(bs, TRANSLATION_TABLES)
	local missing = {}
	for key, value in pairs(tbl["enUS"]) do
		local name 
		if type(value) == "boolean" and value then
			name = key
		else
			name = value
		end
		if not bs:GetSpellIcon(name) then
			table.insert(missing, name)
		end
	end
	if #missing > 0 then
		table.sort(missing)
		print("--- Missing Icons ---")
		for _, name in ipairs(missing) do
			print(name)
		end
		print("--- End Missing Icons ---")
	end
end

local PATH = "dev/Babble-2.2"
local function test(lib)
	local collectgarbage = collectgarbage
	local path = ("%s/%s/%s.lua"):format(PATH, lib, lib)
	collectgarbage()
	local before = collectgarbage("count")
	dofile(path)
	local after = collectgarbage("count")
	collectgarbage()
	local cafter = collectgarbage("count")
	print(lib, after - before, cafter - before)
end



test("Babble-Boss-2.2")
test("Babble-Race-2.2")
test("Babble-Class-2.2")
test("Babble-Faction-2.2")
test("Babble-Fish-2.2")
test("Babble-Gas-2.2")
test("Babble-Herbs-2.2")
test("Babble-Inventory-2.2")
test("Babble-Ore-2.2")
test("Babble-Quest-2.2")
test("Babble-Spell-2.2") --FIXME
test("Babble-SpellTree-2.2")
test("Babble-Tradeskill-2.2")
test("Babble-Trainer-2.2")
test("Babble-Vendor-2.2")
test("Babble-Zone-2.2")
test("Babble-Mount-2.2")

checkspellicons()
